package com.offcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.offcn.mapper.OrdersettingMapper;
import com.offcn.pojo.Ordersetting;
import com.offcn.service.OrdersettingService;
import com.offcn.utils.MessageConstant;
import com.offcn.utils.Result;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: offcnpe_parent
 * @description:
 * @author: xin yi
 * @create: 2021-07-20 16:09
 */
@Service
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Override
    public Result getOrdersettingByDate(String date) {
        String startDate = date + "-1";
        String endDate = date + "-31";
        QueryWrapper<Ordersetting> wrapper = new QueryWrapper<>();
        wrapper.between("orderDate",startDate,endDate);
        List<Ordersetting> ordersettingList = ordersettingMapper.selectList(wrapper);
        ArrayList<Map> data = new ArrayList<>();
        ordersettingList.forEach(i->{
            HashMap map = new HashMap();
            /*z这里的key必须结合页面中需要的json数据的key一致*/
            map.put("date",i.getOrderdate().getDayOfMonth());
            map.put("number",i.getNumber());
            map.put("reservations",i.getReservations());
            data.add(map);

        });
        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,data);
    }

    @Override
    public void uploadTempleatete(List<String[]> stringList) {
        if (stringList != null && stringList.size() > 0){
            for (String[] strings : stringList) {
                Ordersetting ordersetting = new Ordersetting();
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
                LocalDate date = LocalDate.parse(strings[0], dateTimeFormatter);
                ordersetting.setOrderdate(date);
                ordersetting.setNumber(Integer.parseInt(strings[1]));
                QueryWrapper<Ordersetting> wrapper = new QueryWrapper<>();
                wrapper.eq("orderDate",date);
                Ordersetting selectOne = ordersettingMapper.selectOne(wrapper);
                if (selectOne != null){
                    ordersetting.setId(ordersetting.getId());
                    ordersettingMapper.updateById(ordersetting);
                }else {
                    ordersettingMapper.insert(ordersetting);
                }
            }
        }


    }

    @Override
    public Result editNumberByDate(Ordersetting ordersetting) {
        QueryWrapper<Ordersetting> wrapper = new QueryWrapper<>();
        wrapper.eq("orderDate",ordersetting.getOrderdate());
        int row = ordersettingMapper.update(ordersetting, wrapper);
        if (row > 0){
            return new Result(true,MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        }
        return new Result(false,MessageConstant.EDIT_ORDERSETTING_FAIL);
    }
}
